home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / STREAMBU.CC < prev    next >
C/C++ Source or Header  |  1992-03-30  |  5KB  |  230 lines

  1. //    This is part of the iostream library, providing input/output for C++.
  2. //    Copyright (C) 1991, 1992 Per Bothner.
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. //    Library General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU Library General Public
  15. //    License along with this library; if not, write to the Free
  16. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #define _STREAM_COMPAT
  19. #ifdef __GNUG__
  20. #pragma implementation "streambuf.h"
  21. #endif
  22. #include "ioprivate.h"
  23.  
  24. int streambuf::sputn(register const char* s, int n)
  25. {
  26.     register int more = n;
  27.     for (;;) {
  28.     int count = _epptr - _pptr; // Space available.
  29.     if (count > 0) {
  30.         if (count > more)
  31.         count = more;
  32.         if (count > 20) {
  33.         memcpy(_pptr, s, count);
  34.         s += count;
  35.         _pptr += count;
  36.         }
  37.         else if (count <= 0)
  38.         count = 0;
  39.         else {
  40.         register char *p = _pptr;
  41.         for (register int i = count; --i >= 0; ) *p++ = *s++;
  42.         _pptr = p;
  43.         }
  44.         more -= count;
  45.     }
  46.     if (more == 0 || overflow(*s++) == EOF)
  47.         break;
  48.     more--;
  49.     }
  50.     return n - more;
  51. }
  52.  
  53. int streambuf::sgetn(char* s, int n)
  54. {
  55.     register int more = n;
  56.     for (;;) {
  57.     int count = _egptr - _gptr; // Data available.
  58.     if (count > 0) {
  59.         if (count > more)
  60.         count = more;
  61.         if (count > 20) {
  62.         memcpy(s, _gptr, count);
  63.         s += count;
  64.         _gptr += count;
  65.         }
  66.         else if (count <= 0)
  67.         count = 0;
  68.         else {
  69.         register char *p = _gptr;
  70.         for (register int i = count; --i >= 0; ) *s++ = *p++;
  71.         _gptr = p;
  72.         }
  73.         more -= count;
  74.     }
  75.     if (more == 0)
  76.         break;
  77.     int c = underflow();
  78.     if (c == EOF)
  79.         break;
  80.     *s++ = c;
  81.     more--;
  82.     }
  83.     return n - more;
  84. }
  85.  
  86. int streambuf::sync()
  87. {
  88.     if (gptr() == egptr() && pptr() == pbase())
  89.     return 0;
  90.     return EOF;
  91. }
  92.  
  93. int streambuf::pbackfail(int c)
  94. {
  95.     return EOF;
  96. }
  97.  
  98. int streambuf::ungetfail()
  99. {
  100.     if (seekoff(-1, ios::cur, ios::in) == EOF)
  101.     return EOF;
  102.     return sgetc();
  103. }
  104.  
  105. streambuf* streambuf::setbuf(char* p, int len)
  106. {
  107.     setb(p, p+len, 0);
  108.     setp(0, 0);
  109.     setg(0, 0, 0);
  110.     return this;
  111. }
  112.  
  113. streampos streambuf::seekpos(streampos pos, int mode)
  114. {
  115.     return seekoff(pos, ios::beg, mode);
  116. }
  117.  
  118. void streambuf::setb(char* b, char* eb, int a)
  119. {
  120.     if (_base && (_flags & _S_USER_BUF))
  121.     FREE_BUF(_base);
  122.     _base = b;
  123.     _ebuf = eb;
  124.     if (a)
  125.     _flags &= ~_S_USER_BUF;
  126.     else
  127.     _flags |= _S_USER_BUF;
  128. }
  129.  
  130. int streambuf::doallocate()
  131. {
  132.     char *buf = ALLOC_BUF(BUFSIZ);
  133.     if (buf == NULL)
  134.     return EOF;
  135.     setb(buf, buf+BUFSIZ, 1);
  136.     return 1;
  137. }
  138.  
  139. streambuf::streambuf()
  140. {
  141.   _flags = _IO_MAGIC;
  142.   _base = NULL;
  143.   _ebuf = NULL;
  144.   _eback = NULL;
  145.   _gptr = NULL;
  146.   _egptr = NULL;
  147.   _pbase = NULL;
  148.   _pptr = NULL;
  149.   _epptr = NULL;
  150.   _chain = NULL; // Not necessary.
  151. }
  152.  
  153. streambuf::~streambuf()
  154. {
  155.     if (_base && !(_flags & _S_USER_BUF))
  156.     FREE_BUF(_base);
  157. }
  158.  
  159. int streambuf::underflow()
  160. {
  161.     return EOF;
  162. }
  163.  
  164. int streambuf::overflow(int c = EOF)
  165. {
  166.     return EOF;
  167. }
  168.  
  169. streampos
  170. streambuf::seekoff(streamoff, _seek_dir, int mode /*=ios::in|ios::out*/)
  171. {
  172.     return EOF;
  173. }
  174.  
  175. int streambuf::sputbackc(char c)
  176. {
  177.     if (gptr() <= eback()) return pbackfail(c);
  178.     gbump(-1);
  179.     if (*gptr() != c)
  180.     *gptr() = c;
  181.     return (unsigned char)c;
  182. }
  183.  
  184. int streambuf::sungetc()
  185. {
  186.     if (gptr() > eback()) {
  187.     gbump(-1);
  188.     return (unsigned char)*gptr();
  189.     }
  190.     else
  191.     return ungetfail();
  192. }
  193.  
  194. #if 0 /* Work in progress */
  195. void streambuf::collumn(int c)
  196. {
  197.     if (c == -1)
  198.     _collumn = -1;
  199.     else
  200.     _collumn = c - (_pptr - _pbase);
  201. }
  202. #endif
  203.  
  204. int streambuf::flush_all()
  205. {
  206.     int result = 0;
  207.     for (streambuf *sb = _list_all; sb != NULL; sb = sb->xchain())
  208.     if (sb->overflow(EOF) == EOF)
  209.         result = EOF;
  210.     return result;
  211. }
  212.  
  213. void streambuf::flush_all_linebuffered()
  214. {
  215.     for (streambuf *sb = _list_all; sb != NULL; sb = sb->xchain())
  216.     if (sb->linebuffered())
  217.         sb->sync();
  218. }
  219.  
  220.  
  221. #ifdef IO_CLEANUP
  222.   IO_CLEANUP
  223. #else
  224. struct __io_defs {
  225.     __io_defs() { }
  226.     ~__io_defs() { streambuf::flush_all(); }
  227. };   
  228. __io_defs io_defs__;
  229. #endif
  230.